package com.huawei;
import java.util.*;

/**
 * Created by Z-hong on 2024/7/11.
 */

public class HJ68 {
    static class Student{
        String name;
        int score;
        public Student(String name,int score){
            this.name = name;
            this.score = score;
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<Student> list=new ArrayList<>();
        int count =sc.nextInt();
        int sequence=sc.nextInt();
        for (int i = 0; i < count; i++) {
            String name = sc.next();
            int score = sc.nextInt();
            list.add(new Student(name,score));
        }
        if(sequence==0){
            Collections.sort(list, (a,b) ->b.score-a.score);
        }else {
            Collections.sort(list, (a,b) ->a.score-b.score);
        }

        for (Student student : list) {
            System.out.println(student.name+" "+student.score);
        }


    }
}
